home *** CD-ROM | disk | FTP | other *** search
- /*
- File: printing.c
-
- Contains: This file contains the printing routines for the QuickDraw GX shell
- application.
-
- Written by: Developer Technical Support
-
- Copyright: © 1992-1997 by Apple Computer, Inc., all rights reserved.
-
- Writers:
-
- (DMH) Dave Hersey
- (IK) Ingrid Kelly
-
- Change History (most recent first):
-
- <9> 6/1/97 IK Modified printing routines for GXGraphics 1.1.6.
- <8> 8/1/94 DMH Universalized.
- <7> 5/1/94 DMH More cleanup and debugging.
- <6> 3/1/94 DMH General cleanup and debugging.
- <5> 3/1/94 DMH Updated for b4.
- <4> 12/1/93 DMH Updated for b3.
- <3> 9/1/93 PLA Updated to run with the b2 "GXified" interface files.
- <2> 2/1/93 DMH Debugged and plopped on GX CD.
- <1> 9/1/92 DMH First created.
- */
-
- #include <stdio.h>
- #include <Collections.h>
-
- #include "CodecLibrary.h"
- #include "FontLibrary.h"
- #include "GraphicsLibraries.h"
-
- #include "main.h"
-
- /* ---------------------------------------------------------------------------
- Function prototypes
- --------------------------------------------------------------------------- */
-
- static void SetUpEditMenuRec(gxEditMenuRecord *menuRec);
- static void FormatPageShape(gxprFormatHdl gxprFormat, gxShape shape);
-
- /* ===========================================================================
- Public functions
- =========================================================================== */
-
- /* ---------------------------------------------------------------------------
- DoPageFormat
- This routine performs GX's equivalent of the Page Setup (PrStlDialog) call
- of the old printing architecture.
- --------------------------------------------------------------------------- */
-
- OSErr DoPageFormat(WindowPtr window, gxDialogResult *result)
- {
- OSErr error = noErr;
-
- *result = nil;
-
- /* If we have a non-nil WindowPtr, set up our edit menu record and handle
- the job format dialog. Remember to check for errors.
- */
- if ( window != nil )
- {
- TH_Document document;
- gxprJobHdl gxprJob;
- gxEditMenuRecord edMenuRec;
-
- document = GetDocument(window);
- gxprJob = GetDocumentJob(document);
-
- /* Fill in the location of the application's Edit menu items. */
-
- SetUpEditMenuRec(&edMenuRec);
-
- *result = GXPrJobDefaultFormatDialog(gxprJob, &edMenuRec);
- error = GXPrGetJobError(gxprJob);
- }
-
- return error;
- }
-
- /* ---------------------------------------------------------------------------
- DoCustomPageFormat
- This routine performs a by-page format. In other words, it sets formatting
- info for only the current page.
- --------------------------------------------------------------------------- */
-
- OSErr DoCustomPageFormat(WindowPtr window, gxDialogResult *result)
- {
- OSErr error = noErr;
-
- /* If we have a non-nil WindowPtr, set up our edit menu record and handle
- the job format dialog. Remember to check for errors.
- */
- if ( window != nil )
- {
- TH_Document document;
- gxprJobHdl gxprJob;
- gxprFormatHdl gxprFormat;
- Boolean isClonedFormat = false;
- gxEditMenuRecord edMenuRec;
-
- document = GetDocument(window);
- gxprJob = GetDocumentJob(document);
- gxprFormat = GetPageFormat(document, (*document)->page);
-
- /* Use the default page format of the document print job. */
-
- if ( gxprFormat == nil )
- {
- gxprFormat = GXPrNewFormat(gxprJob);
- error = GXPrGetJobError(gxprJob);
-
- if ( error != noErr )
- goto GXPrNewFormatFailed;
-
- isClonedFormat = true;
- }
-
- /* Fill in the location of the application's Edit menu items. */
-
- SetUpEditMenuRec(&edMenuRec);
-
- *result = GXPrFormatDialog(gxprFormat, &edMenuRec, nil);
-
- switch ( *result )
- {
- /* If the user chooses Remove, store the default format for this page. */
-
- case gxRevertSelected:
- GXPrDisposeFormat(gxprFormat);
- SetPageFormat(document, (*document)->page, nil);
- break;
-
- case gxOKSelected:
- SetPageFormat(document, (*document)->page, gxprFormat);
- break;
-
- /* If the user chooses Cancel, dispose of the cloned copy of the default format. */
-
- case gxCancelSelected:
- if ( isClonedFormat )
- GXPrDisposeFormat(gxprFormat);
- break;
- }
-
- error = GXPrGetJobError(gxprJob);
- }
-
- GXPrNewFormatFailed:
- return error;
- }
-
- /* ---------------------------------------------------------------------------
- DoPrint
- This routine performs GX's equivalent of the PrJobDialog call in the old
- printing architecture, and then prints the document if the user wants to.
- --------------------------------------------------------------------------- */
-
- OSErr DoPrint(WindowPtr window)
- {
- OSErr error;
-
- /* If we have a non-nil WindowPtr, set up our edit menu record and handle
- the print job dialog. Remember to check for errors. If no errors occur,
- and the user clicks ok, print the window's document.
- */
- if ( window != nil )
- {
- TH_Document document;
- gxprJobHdl gxprJob;
- gxEditMenuRecord edMenuRec;
- gxDialogResult result;
-
- document = GetDocument(window);
- gxprJob = GetDocumentJob(document);
-
- SetUpEditMenuRec(&edMenuRec);
-
- result = GXPrJobPrintDialog(gxprJob, &edMenuRec);
- error = GXPrGetJobError(gxprJob);
-
- if ( result == gxOKSelected && error == noErr )
- error = DoPrintLoop(window);
- }
-
- return error;
- }
-
- /* ---------------------------------------------------------------------------
- DoPrintOneCopy
- This routine sets up our job collection items for printing one copy of a
- document, and then prints the document.
- --------------------------------------------------------------------------- */
-
- OSErr DoPrintOneCopy(WindowPtr window)
- {
- gxprJobHdl gxprJob = nil;
- gxCopiesInfo *oldCopiesInfoPtr = nil;
- gxFileDestinationInfo *oldFileDestinationInfoPtr = nil;
- gxPageRangeInfo *oldPageRangeInfoPtr = nil;
- long oldPageRangeInfoSize = 0;
- OSErr error = noErr;
-
- /* If we have a non-nil WindowPtr, start the print job, print a page and
- then finish the job. Remember to check those errors!
- */
- if ( window != nil )
- {
- TH_Document document;
- gxCopiesInfo copiesInfo;
- gxFileDestinationInfo fileDestinationInfo;
- gxPageRangeInfo pageRangeInfo;
-
- document = GetDocument(window);
- gxprJob = GetDocumentJob(document);
-
- /* Set the copies item to "one". */
-
- copiesInfo.copies = 1;
- error = GXPrSetJobCopies(gxprJob, &copiesInfo, &oldCopiesInfoPtr);
-
- if ( error != noErr )
- goto GXPrSetJobCopiesFailed;
-
- /* Set the page range item to "all". */
-
- pageRangeInfo.simpleRange.optionChosen = gxDefaultPageRange;
- pageRangeInfo.simpleRange.fromPage = 1;
- pageRangeInfo.simpleRange.toPage = (*document)->pageCount;
- pageRangeInfo.simpleRange.printAll = true;
- pageRangeInfo.fromString[0] = '\0';
- pageRangeInfo.toString[0] = '\0';
- pageRangeInfo.minFromPage = 1;
- pageRangeInfo.maxToPage = (*document)->pageCount;
- pageRangeInfo.replaceString[0] = '\0';
-
- oldPageRangeInfoSize = sizeof(gxPageRangeInfo);
-
- error = GXPrSetJobPageRange(gxprJob, sizeof(gxPageRangeInfo), &pageRangeInfo, &oldPageRangeInfoSize, &oldPageRangeInfoPtr);
-
- if ( error != noErr )
- goto GXPrSetJobPageRangeFailed;
-
- /* Set the file destination item to "printer". */
-
- fileDestinationInfo.toFile = false;
- error = GXPrSetJobFileDestination(gxprJob, &fileDestinationInfo, &oldFileDestinationInfoPtr);
-
- if ( error != noErr )
- goto GXPrSetJobFileDestinationFailed;
-
- /* Print one copy of the document. */
-
- error = DoPrintLoop(window);
- }
-
- /* Restore original number of copies, page range, and output destination. */
-
- GXPrSetJobCopiesFailed:
- if ( oldCopiesInfoPtr != nil )
- {
- (void) GXPrSetJobCopies(gxprJob, oldCopiesInfoPtr, nil);
- DisposePtr((Ptr)oldCopiesInfoPtr);
- }
-
- GXPrSetJobPageRangeFailed:
- if ( oldPageRangeInfoPtr != nil )
- {
- (void) GXPrSetJobPageRange(gxprJob, oldPageRangeInfoSize, oldPageRangeInfoPtr, 0, nil);
- DisposePtr((Ptr)oldPageRangeInfoPtr);
- }
-
- GXPrSetJobFileDestinationFailed:
- if ( oldFileDestinationInfoPtr != nil )
- {
- (void) GXPrSetJobFileDestination(gxprJob, oldFileDestinationInfoPtr, nil);
- DisposePtr((Ptr)oldFileDestinationInfoPtr);
- }
-
- return error;
- }
-
- /* ---------------------------------------------------------------------------
- DoPrintLoop
- This routine prints the window's document using whatever job and format is
- currently attached to it. (If the window wwere just created and then the
- user selected Print One Copy w/o first selecting Page Setup…, the system
- default job and format are stored with this document.
- --------------------------------------------------------------------------- */
-
- OSErr DoPrintLoop(WindowPtr window)
- {
- OSErr error = noErr;
-
- /* If we have a non-nil WindowPtr, start the print job, print a page and
- then finish the job. Remember to check those errors!
- */
- if ( window != nil )
- {
- TH_Document document;
- gxprJobHdl gxprJob;
- gxViewPort viewport;
- Str255 title;
- gxprFormatHdl gxprFormat;
- gxShape shape;
- long firstPage;
- long lastPage;
- long pg;
- long pageCount;
-
- document = GetDocument(window);
- gxprJob = GetDocumentJob(document);
- viewport = GetDocumentViewPort(document);
-
- GetWTitle(window, title);
-
- /* Determine which pages the user selected to print. If the user specifies
- a range that is greater than the actual number of pages, then only print
- those pages that are in the document.
- */
- GXPrGetJobPageRange(gxprJob, &firstPage, &lastPage);
-
- pageCount = (*document)->pageCount;
-
- if ( firstPage < 1 )
- firstPage = 1;
- else if ( firstPage > pageCount )
- firstPage = pageCount;
-
- if ( lastPage < firstPage )
- lastPage = firstPage;
- else if ( lastPage > pageCount )
- lastPage = pageCount;
-
- /* Calculate the total number of pages to print. */
-
- pageCount = lastPage - firstPage + 1;
-
- error = GXPrGetJobError(gxprJob);
-
- /* Begin printing if there are no errors. */
-
- if ( error == noErr )
- {
- GXPrStartJob(gxprJob, title, pageCount);
- error = GXPrGetJobError(gxprJob);
-
- for ( pg = firstPage; error == noErr && pg <= lastPage; pg++ )
- {
- gxprFormat = GetPageFormat(document, pg);
- shape = GXCopyToShape(nil, GetPageShape(document, pg));
-
- FormatPageShape(gxprFormat, shape);
-
- #if 0
- /* For each page, call the GXPrintPage function for the page's picture
- shape.
- */
- GXPrPrintPage(gxprJob, pg, gxprFormat, shape);
- error = GXPrGetJobError(gxprJob);
- #else
- /* For each page, call the GXStartPage function, draw the page, and
- then call the GXFinishPage function. In this example, only a single
- shape is drawn for each page.
- */
- GXPrStartPage(gxprJob, pg, gxprFormat, 1, &viewport);
- error = GXPrGetJobError(gxprJob);
-
- /* Draw the data for the page if there are no errors. */
-
- if ( error == noErr )
- {
- GXDrawShape(shape);
- error = (OSErr)GXGetGraphicsError(nil);
- }
-
- if ( error == noErr )
- GXPrFinishPage(gxprJob);
- #endif
-
- if ( shape != nil )
- GXDisposeShape(shape);
- }
-
- /* Finish printing. */
-
- GXPrFinishJob(gxprJob);
-
- if ( error == noErr )
- error = GXPrGetJobError(gxprJob);
-
- }
- }
-
- return error;
- }
-
- /* ===========================================================================
- Private functions
- =========================================================================== */
-
- /* ---------------------------------------------------------------------------
- SetUpEditMenuRec
- This routine sets up an gxEditMenuRecord which references our edit menu.
- This structure is used by the GXJobDefaultFormatDialog and
- GXJobPrintDialog calls to allow cut, copy, and paste operations from the
- dialogs.
- --------------------------------------------------------------------------- */
-
- static void SetUpEditMenuRec(gxEditMenuRecord *edMenuRec)
- {
- edMenuRec->editMenuID = mEdit;
- edMenuRec->cutItem = iCut;
- edMenuRec->copyItem = iCopy;
- edMenuRec->pasteItem = iPaste;
- edMenuRec->clearItem = iClear;
- edMenuRec->undoItem = iUndo;
- }
-
- /* ---------------------------------------------------------------------------
- FormatPageShape
- --------------------------------------------------------------------------- */
-
- static void FormatPageShape(gxprFormatHdl gxprFormat, gxShape shape)
- {
- Collection formatCollection;
- T_CustomCollection customConfig;
- gxShape textShape;
- Str255 textStr;
- long numChars;
- OSErr error;
-
- if ( gxprFormat == nil )
- return;
-
- if ( shape == nil )
- return;
-
- formatCollection = GXPrGetFormatCollection(gxprFormat);
- error = GXPrGetJobError(GXPrGetFormatJob(gxprFormat));
-
- if ( formatCollection == nil || error != noErr )
- goto GXPrGetFormatCollectionFailed;
-
- error = GetCollectionItem(formatCollection, kCustomCollectionType, gxPrintingTagID, nil, &customConfig);
-
- if ( error != noErr )
- goto GetCollectionItemFailed;
-
- if ( customConfig.one || customConfig.two || customConfig.three )
- {
- textShape = GXNewShape(gxTextType);
- GXSetShapeTextSize(textShape, ff(32));
- SetShapeCommonFont(textShape, timesFont);
- GXMoveShapeTo(textShape, ff(10), ff(40));
-
- if ( customConfig.one ) /* Enable page shuffling */
- {
- numChars = sprintf((Ptr)&textStr[0], "%s", "Enable page shuffling.");
- GXSetText(textShape, numChars, textStr, nil);
- AddToShape(shape, textShape);
- GXMoveShape(textShape, ff(0), ff(40));
- }
-
- if ( customConfig.two ) /* Display page numbers */
- {
- numChars = sprintf((Ptr)&textStr[0], "%s", "Display page numbers.");
- GXSetText(textShape, numChars, textStr, nil);
- AddToShape(shape, textShape);
- GXMoveShape(textShape, ff(0), ff(40));
- }
-
- if ( customConfig.three != 0 ) /* Flip image */
- {
- numChars = sprintf((Ptr)&textStr[0], "%s%d%s", "Flip image by ", customConfig.three, " degrees.");
- GXSetText(textShape, numChars, textStr, nil);
- AddToShape(shape, textShape);
- }
-
- GXDisposeShape(textShape);
- }
-
- GetCollectionItemFailed:
- GXPrGetFormatCollectionFailed:
- return;
- }
-